package com.gcd;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.gcd.inference.Machine;
import com.gcd.knowledge.Node;

public class Solve extends Activity {

	private final SolveInterviewer forms[] = { new HartaInterviewer(),
			new JKMawarisInterviewer(), new AnakInterviewer(),
			new JanduInterviewer(), new OrtuInterviewer(),
			new SaudaraInterviewer(), new CucuInterviewer() };
	private int currForm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Clear fact
		Global.facts.clear();
		
		// Clear Intermediate Result
		Global.intermediateResult.clear();
		
		// Set current form number
		currForm = 0;
		
		// Set first form view
		setContentView(forms[currForm].getLayout());
	}

	public void btnNextClick(View v) {
		// Add fact from current form
		forms[currForm].addFact();
		
		// Increment form number
		++currForm;
		if (currForm >= forms.length) {
			currForm = forms.length - 1;
		}
		
		// Set form view
		setContentView(forms[currForm].getLayout());
	}

	public void btnPrevClick(View v) {
		// Decrement form number
		--currForm;
		if (currForm < 0) {
			currForm = 0;
		}

		// Set form view
		setContentView(forms[currForm].getLayout());
	}

	public void btnFinishClick(View v) {
		// Add fact from current form
		forms[currForm].addFact();
		
		// Print fact
		Log.d("sout", "facts : " + Global.facts);
		
		try {
			// Do Inference
			(new Machine()).doInference();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// Explain it!
		explanation();
	}
	
	public void radioButtonYaClick(View v) {
		EditText et =  (EditText)findViewById(R.id.editTextCucuLaki);
		et.setEnabled(true);
		et =  (EditText)findViewById(R.id.editTextCucuPerempuan);
		et.setEnabled(true);
	}
	
	public void radioButtonTidakClick(View v) {
		EditText et =  (EditText)findViewById(R.id.editTextCucuLaki);
		et.setEnabled(false);
		et =  (EditText)findViewById(R.id.editTextCucuPerempuan);
		et.setEnabled(false);
	}
	
	private void explanation() {
		// Set view
		setContentView(R.layout.solve_explanation);
		
		StringBuilder sb = new StringBuilder();
		Pair iR = Global.intermediateResult;
		TextView tv;
		
		// Bagian Harta
		tv = (TextView) findViewById(R.id.textViewDescHarta);
		tv.setText(Global.facts.get("harta"));
		
		// Bagian Anak
		//sb.append("Anak laki-laki : ").append(iR.get("bagianAnakLaki")).append("\n");
		//sb.append("Anak perempuan : ").append(iR.get("bagianAnakPerempuan")).append("\n");
		if (iR.get("bagianAnak")== null) {
			sb.append("-").append("\n");
		} else {
			sb.append(iR.get("bagianAnak")).append("\n");
		}
		sb.append(iR.get("explanation tree 0"));
		
		tv = (TextView)findViewById(R.id.textViewDescAnak);
		tv.setText(sb.toString());
		
		// Bagian Suami / Istri
		sb = new StringBuilder();
		sb.append("Suami : ").append(iR.get("bagianSuami")).append("\n");
		sb.append("Istri : ").append(iR.get("bagianIstri")).append("\n");
		sb.append(iR.get("explanation tree 1"));
		tv = (TextView)findViewById(R.id.textViewDescJandu);
		tv.setText(sb.toString());
		
		// Bagian Orangtua
		sb = new StringBuilder();
		sb.append("Bapak : ").append(iR.get("bagianBapak")).append("\n");
		sb.append("Ibu   : ").append(iR.get("bagianIbu")).append("\n");
		sb.append(iR.get("explanation tree 2"));
		tv = (TextView)findViewById(R.id.textViewDescOrtu);
		tv.setText(sb.toString());
		
		// Bagian Saudara
		int jumlahSaudaraLaki, jumlahSaudaraPerempuan; 
		try {
			jumlahSaudaraLaki = Integer.parseInt(Global.facts.get("jumlah saudara laki"));
		} catch (Exception e) {
			jumlahSaudaraLaki = 0;
		}
		
		try {
			jumlahSaudaraPerempuan = Integer.parseInt(Global.facts.get("jumlah saudara perempuan"));
		} catch (Exception e) {
			jumlahSaudaraPerempuan = 0;
		}
			
		sb = new StringBuilder();
		sb.append(jumlahSaudaraLaki +  " saudara laki-laki masing-masing memperoleh ").append(iR.get("bagianSaudaraLaki")).append("\n");
		sb.append(jumlahSaudaraPerempuan + " saudara perempuan masing-masing memperoleh ").append(iR.get("bagianSaudaraPerempuan")).append("\n");
		sb.append(iR.get("explanation tree 3"));
		tv = (TextView) findViewById(R.id.textViewDescSaudara);
		tv.setText(sb.toString());
		
		
		// Bagian Cucu
		int jumlahCucuLaki, jumlahCucuPerempuan;
		try {
			jumlahCucuLaki = Integer.parseInt(Global.facts.get("jumlah cucu laki"));
		} catch (Exception e) {
			jumlahCucuLaki = 0;
		}
		
		try {
			jumlahCucuPerempuan = Integer.parseInt(Global.facts.get("jumlah cucu perempuan"));
		} catch (Exception e) {
			jumlahCucuPerempuan = 0;
		}
		
		sb = new StringBuilder();
		sb.append(jumlahCucuLaki + " cucu laki-laki masing-masing memperoleh ").append(iR.get("bagianCucuLaki")).append("\n");
		sb.append(jumlahCucuPerempuan + "cucu perempuan masing-masing memperoleh ").append(iR.get("bagianCucuPerempuan")).append("\n");
		sb.append(iR.get("explanation tree 4"));
		tv = (TextView)findViewById(R.id.textViewDescCucu);
		tv.setText(sb.toString());
		
		// Sistem aul / aum
		if (iR.containsKey("aul")) {
			// Title
			tv = (TextView) findViewById(R.id.textViewTitleSistem);
			tv.setText("Sistem Aul");
			tv.setVisibility(View.VISIBLE);

			// Desc
			tv = (TextView) findViewById(R.id.textViewDescSistem);
			tv.setText(iR.get("aul"));
			tv.setVisibility(View.VISIBLE);
		} else if (iR.containsKey("aum")) {
			// Title
			tv = (TextView) findViewById(R.id.textViewTitleSistem);
			tv.setText("Sistem Aum");
			tv.setVisibility(View.VISIBLE);

			// Desc
			tv = (TextView) findViewById(R.id.textViewDescSistem);
			tv.setText(iR.get("aum"));
			tv.setVisibility(View.VISIBLE);
		}
	}
	
	public abstract class SolveInterviewer {
		protected int layout = -1;

		public SolveInterviewer(int layout) {
			this.layout = layout;
		}

		public abstract void addFact();

		public int getLayout() {
			return layout;
		}

		public void setLayout(int value) {
			this.layout = value;
		}
	}
	
	public class JKMawarisInterviewer extends SolveInterviewer {
		public JKMawarisInterviewer() {
			super(R.layout.solve_int_jenis_kelamin);
		}

		@Override
		public void addFact() {
			// Get radio group
			RadioGroup radioGroupJK = (RadioGroup)findViewById(R.id.radioGroupJK);
			
			// Get checked
			int checkedId = radioGroupJK.getCheckedRadioButtonId();
			
			// Add to facts
			if (checkedId == R.id.radioButttonLaki) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_PEWARIS, Node.ANSWER.LAKI_LAKI);
			} else if (checkedId == R.id.radioButtonPerempuan) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_PEWARIS, Node.ANSWER.PEREMPUAN);
			}
		}
	}

	public class HartaInterviewer extends SolveInterviewer {
		public HartaInterviewer() {
			super(R.layout.solve_int_harta);
		}

		@Override
		public void addFact() {
			// Get harta edit Text
			EditText editTextHarta = (EditText) findViewById(R.id.editTextHarta);
			
			// Add fact harta
			Global.facts.put("harta", editTextHarta.getText().toString());
			Log.d("sout", "harta : " + Global.facts.get("harta"));
		}
	}

	public class AnakInterviewer extends SolveInterviewer {
		public AnakInterviewer() {
			super(R.layout.solve_int_anak_1);
		}

		@Override
		public void addFact() {
			EditText edtTxtLaki = (EditText) findViewById(R.id.editText1);
			EditText edtTxtPerempuan = (EditText) findViewById(R.id.editText2);

			// Init
			int intLaki = 0;
			int intPerempuan = 0;
			Global.facts.put("jumlah anak laki", intLaki);
			Global.facts.put("jumlah anak perempuan", intPerempuan);
			
			try {
				intLaki = Integer.parseInt(edtTxtLaki.getText().toString());
				intPerempuan = Integer.parseInt(edtTxtPerempuan.getText().toString());
			} catch (Exception e) {
				// Assume input is empty
				Global.facts.put(Node.QUESTION.HAS_ANAK, Node.ANSWER.NO);
				return;
			}

			// Tidak ada anak
			if (intLaki == 0 && intPerempuan == 0) {
				Global.facts.put(Node.QUESTION.HAS_ANAK, Node.ANSWER.NO);
				return;
			}

			// Ada Anak
			Global.facts.put(Node.QUESTION.HAS_ANAK, Node.ANSWER.YES);
			Global.facts.put("jumlah anak laki", intLaki);
			Global.facts.put("jumlah anak perempuan", intPerempuan);

			if (intLaki + intPerempuan == 1) {
				Global.facts.put(Node.QUESTION.JUMLAH_ANAK, Node.ANSWER.SATU);
			} else {
				Global.facts.put(Node.QUESTION.JUMLAH_ANAK, Node.ANSWER.N);
			}

			if (intPerempuan == 0) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_ANAK,
						Node.ANSWER.LAKI_LAKI);
				return;
			} else if (intLaki == 0) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_ANAK,
						Node.ANSWER.PEREMPUAN);
				return;
			} else {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_ANAK,
						Node.ANSWER.LAKI_LAKI_PEREMPUAN);
			}
		}
	}

	public class JanduInterviewer extends SolveInterviewer {
		public JanduInterviewer() {
			super(R.layout.solve_int_jandu_1);
		}

		@Override
		public void addFact() {
			// Get radio group
			RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
			
			// Get checked
			int checkedId = radioGroup.getCheckedRadioButtonId();
			
			// Add fact
			if (checkedId == R.id.radioButtonYa) {
				Global.facts.put(Node.QUESTION.HAS_ISTRI, Node.ANSWER.YES);
				Global.facts.put(Node.QUESTION.HAS_SUAMI, Node.ANSWER.YES);
			} else if (checkedId == R.id.radioButtonTidak) {
				Global.facts.put(Node.QUESTION.HAS_ISTRI, Node.ANSWER.NO);
				Global.facts.put(Node.QUESTION.HAS_SUAMI, Node.ANSWER.NO);
			}
		}
	}

	public class OrtuInterviewer extends SolveInterviewer {
		public OrtuInterviewer() {
			super(R.layout.solve_int_ortu_1);
		}

		@Override
		public void addFact() {
			// Get Radio group
			RadioGroup radioGroupAyah = (RadioGroup) findViewById(R.id.radioGroupAyah);
			RadioGroup radioGroupIbu = (RadioGroup) findViewById(R.id.radioGroupIbu);
			
			// Get checked ayah
			int checkedAyah = radioGroupAyah.getCheckedRadioButtonId();
			int checkedIbu  = radioGroupIbu.getCheckedRadioButtonId();
			
			// Add fact has ayah
			if (checkedAyah == R.id.radioButtonAyahYa) {
				Global.facts.put(Node.QUESTION.HAS_BAPAK, Node.ANSWER.YES);
			} else if (checkedAyah == R.id.radioButtonAyahTidak) {
				Global.facts.put(Node.QUESTION.HAS_BAPAK, Node.ANSWER.NO);
			}
			
			// Add fact has ibu
			if (checkedIbu == R.id.radioButtonIbuYa) {
				Global.facts.put(Node.QUESTION.HAS_IBU, Node.ANSWER.YES);
			} else if (checkedIbu == R.id.radioButtonIbuTidak) {
				Global.facts.put(Node.QUESTION.HAS_IBU, Node.ANSWER.NO);
			}
		}
	}

	public class SaudaraInterviewer extends SolveInterviewer {
		public SaudaraInterviewer() {
			super(R.layout.solve_int_saudara_1);
		}

		@Override
		public void addFact() {
			EditText editTextLaki = (EditText)findViewById(R.id.editTextSaudaraLaki);
			EditText editTextPerempuan = (EditText)findViewById(R.id.editTextSaudaraPerempuan);
			
			int intLaki;
			int intPerempuan;
			
			try {
				intLaki = Integer.parseInt(editTextLaki.getText().toString());
				intPerempuan = Integer.parseInt(editTextPerempuan.getText().toString());
			} catch (Exception e) {
				// Assume input is empty
				Global.facts.put(Node.QUESTION.HAS_SAUDARA, Node.ANSWER.NO);
				return;
			}

			// Tidak ada anak
			if (intLaki == 0 && intPerempuan == 0) {
				Global.facts.put(Node.QUESTION.HAS_SAUDARA, Node.ANSWER.NO);
				return;
			}

			// Ada Anak
			Global.facts.put(Node.QUESTION.HAS_SAUDARA, Node.ANSWER.YES);
			Global.facts.put("jumlah saudara laki", intLaki);
			Global.facts.put("jumlah saudara perempuan", intPerempuan);

			if (intLaki + intPerempuan == 1) {
				Global.facts.put(Node.QUESTION.JUMLAH_SAUDARA, Node.ANSWER.SATU);
			} else {
				Global.facts.put(Node.QUESTION.JUMLAH_SAUDARA, Node.ANSWER.N);
			}

			if (intPerempuan == 0) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_SAUDARA,
						Node.ANSWER.LAKI_LAKI);
				return;
			} else if (intLaki == 0) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_SAUDARA,
						Node.ANSWER.PEREMPUAN);
				return;
			} else {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_SAUDARA,
						Node.ANSWER.LAKI_LAKI_PEREMPUAN);
			}
		}
	}

	public class CucuInterviewer extends SolveInterviewer {
		public CucuInterviewer() {
			super(R.layout.solve_int_cucu_1);
		}

		@Override
		public void addFact() {
			// Get Radio group
			RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
			
			// Get Chekced
			int checkedId = radioGroup.getCheckedRadioButtonId();
			
			if (checkedId == R.id.radioButtonYa) {
				Global.facts.put(Node.QUESTION.HAS_ANAK_MENINGGAL, Node.ANSWER.YES);
			} else if (checkedId == R.id.radioButtonTidak) {
				Global.facts.put(Node.QUESTION.HAS_ANAK_MENINGGAL, Node.ANSWER.NO);
				return;
			}
			
			// Get jumlah cucu
			EditText editTextLaki = (EditText) findViewById(R.id.editTextCucuLaki);
			EditText editTextPerempuan = (EditText) findViewById(R.id.editTextCucuPerempuan);

			// Init
			int intLaki;
			int intPerempuan;
			
			try {
				intLaki = Integer.parseInt(editTextLaki.getText().toString());
				intPerempuan = Integer.parseInt(editTextPerempuan.getText().toString());
			} catch (Exception e) {
				// Assume input is empty
				Global.facts.put(Node.QUESTION.HAS_CUCU, Node.ANSWER.NO);
				return;
			}
			
			// Tidak ada cucu
			if (intLaki == 0 && intPerempuan == 0) {
				Global.facts.put(Node.QUESTION.HAS_CUCU, Node.ANSWER.NO);
				return;
			}

			// Ada cucu
			Global.facts.put(Node.QUESTION.HAS_CUCU, Node.ANSWER.YES);
			Global.facts.put("jumlah cucu laki", intLaki);
			Global.facts.put("jumlah cucu perempuan", intPerempuan);

			if (intLaki + intPerempuan == 1) {
				Global.facts.put(Node.QUESTION.JUMLAH_CUCU, Node.ANSWER.SATU);
			} else {
				Global.facts.put(Node.QUESTION.JUMLAH_CUCU, Node.ANSWER.N);
			}
			
			if (intPerempuan == 0) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_CUCU,
						Node.ANSWER.LAKI_LAKI);
				return;
			} else if (intLaki == 0) {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_CUCU,
						Node.ANSWER.PEREMPUAN);
				return;
			} else {
				Global.facts.put(Node.QUESTION.JENIS_KELAMIN_CUCU,
						Node.ANSWER.LAKI_LAKI_PEREMPUAN);
			}
		}
	}
}
